package router

import (
	"gdshop-back-end-go/client-api/app/controllers"
	"gdshop-back-end-go/common/libs/token"
	"gdshop-back-end-go/common/response"
	"github.com/SongLiangChen/RateLimiter"
	"github.com/gogf/gf/frame/g"
	"github.com/gogf/gf/net/ghttp"
	"github.com/gogf/gf/os/glog"
)

var logger *glog.Logger

// 你可以将路由注册放到一个文件中管理，
// 也可以按照模块拆分到不同的文件中管理，
// 但统一都放到router目录下。
func init() {
	versionName := g.Cfg().GetString("site.ApiVersionName")
	defaultVersionName := g.Cfg().GetString("site.ApiDefaultVersionName")
	logger = g.Log()
	s := g.Server()

	allRules := RateLimiter.NewRules()
	// 全站限流
	allRules.AddRule("all", &RateLimiter.Rule{
		Duration: 1,   // 秒
		Limit:    100, // 次
	})
	// 提交订单限流
	allRules.AddRule("/order/submit", &RateLimiter.Rule{
		Duration: 1,  // 秒
		Limit:    20, // 次
	})

	rateLimiterRedisCnf := g.Cfg().GetStrings("redis.rateLimiter")
	if len(rateLimiterRedisCnf) < 1 {
		panic("redis.rateLimiter 配置错误")
	}
	allRateLimiter, rlIsOk := RateLimiter.NewRateLimiter("redis")
	if rlIsOk == false {
		panic("限流器加载失败")
	}
	err := allRateLimiter.InitRules(allRules, rateLimiterRedisCnf...)
	if err != nil {
		panic(err)
	}

	s.Use(MiddlewareErrorHandler)
	// 限流器
	s.Use(func(r *ghttp.Request) {
		// 不限用户，全站限流
		if !allRateLimiter.TokenAccess("all", "all") {
			r.Response.ClearBuffer()
			r.Response.WriteJsonExit(response.FailByRequestRateLimiter(r))
			return
		}
		// 不限用户，根据URI 限流
		if !allRateLimiter.TokenAccess("all", r.Request.URL.Path) {
			r.Response.ClearBuffer()
			r.Response.WriteJsonExit(response.FailByRequestRateLimiter(r))
			return
		}

		r.Middleware.Next()
	})

	s.Group("/", func(group *ghttp.RouterGroup) {
		homeControllerMaps := map[string]interface{}{
			"1.0.0": new(controllers.HomeController),
		}
		// 首页
		group.GET("/home", func(r *ghttp.Request) {
			BindHandlerVersion(r, versionName, homeControllerMaps, defaultVersionName, "Index")
		})
		// 装修预览
		group.GET("/preview", func(r *ghttp.Request) {
			BindHandlerVersion(r, versionName, homeControllerMaps, defaultVersionName, "Preview")
		})
		// 登陆
		group.POST("/login", func(r *ghttp.Request) {
			BindHandlerVersion(r, versionName, homeControllerMaps, defaultVersionName, "Login")
		})
		// 注册
		group.POST("/register", func(r *ghttp.Request) {
			BindHandlerVersion(r, versionName, homeControllerMaps, defaultVersionName, "Register")
		})
		// 找回密码
		group.POST("/forget", func(r *ghttp.Request) {
			BindHandlerVersion(r, versionName, homeControllerMaps, defaultVersionName, "Forget")
		})
		// 发送验证码
		group.GET("/send_vcode", func(r *ghttp.Request) {
			BindHandlerVersion(r, versionName, homeControllerMaps, defaultVersionName, "SendVCode")
		})
		// 快速登录、第三方登录
		group.POST("/quick_login", func(r *ghttp.Request) {
			BindHandlerVersion(r, versionName, homeControllerMaps, defaultVersionName, "QuickLogin")
		})
		// 省市区查询
		group.GET("/area", func(r *ghttp.Request) {
			BindHandlerVersion(r, versionName, homeControllerMaps,
				defaultVersionName, "Area")
		})

		group.GET("/pcas", func(r *ghttp.Request) {
			BindHandlerVersion(r, versionName, homeControllerMaps,
				defaultVersionName, "Pcas")
		})

		group.GET("/slides", func(r *ghttp.Request) {
			BindHandlerVersion(r, versionName, homeControllerMaps,
				defaultVersionName, "Slides")
		})

		group.POST("/wechat_info", func(r *ghttp.Request) {
			BindHandlerVersion(r, versionName, homeControllerMaps,
				defaultVersionName, "WechatInfo")
		})

		group.POST("/wechat_share", func(r *ghttp.Request) {
			BindHandlerVersion(r, versionName, homeControllerMaps,
				defaultVersionName, "WechatShare")
		})

		group.POST("/wechat_login", func(r *ghttp.Request) {
			BindHandlerVersion(r, versionName, homeControllerMaps,
				defaultVersionName, "WechatLogin")
		})

		group.POST("/wechat_mp_login", func(r *ghttp.Request) {
			BindHandlerVersion(r, versionName, homeControllerMaps,
				defaultVersionName, "WechatMpLogin")
		})

		group.GET("/share_url_to_page_path", func(r *ghttp.Request) {
			BindHandlerVersion(r, versionName, homeControllerMaps,
				defaultVersionName, "ShareUrlToPagePath")
		})

		group.GET("/captcha", func(r *ghttp.Request) {
			BindHandlerVersion(r, versionName, homeControllerMaps,
				defaultVersionName, "Captcha")
		})

		group.POST("/captcha_check", func(r *ghttp.Request) {
			BindHandlerVersion(r, versionName, homeControllerMaps,
				defaultVersionName, "CaptchaCheck")
		})
	})
	s.Group("/", func(group *ghttp.RouterGroup) {
		controllerMaps := map[string]interface{}{
			"1.0.0": new(controllers.HomeController),
		}
		group.Middleware(MiddlewareIsMemberLogin)
		group.GET("/layout", func(r *ghttp.Request) {
			BindHandlerVersion(r, versionName, controllerMaps, defaultVersionName, "Layout")
		})

		group.GET("/hot_searchs", func(r *ghttp.Request) {
			BindHandlerVersion(r, versionName, controllerMaps, defaultVersionName, "HotSearchs")
		})
	})

	s.Group("/cart", func(group *ghttp.RouterGroup) {
		controllerMaps := map[string]interface{}{
			"1.0.0": new(controllers.CartController),
		}
		group.Middleware(MiddlewareIsMemberLogin)
		group.POST("/save", func(r *ghttp.Request) {
			BindHandlerVersion(r, versionName, controllerMaps, defaultVersionName, "Save")
		})

		group.GET("/del", func(r *ghttp.Request) {
			BindHandlerVersion(r, versionName, controllerMaps, defaultVersionName, "Del")
		})
		group.POST("/save_selected", func(r *ghttp.Request) {
			BindHandlerVersion(r, versionName, controllerMaps, defaultVersionName,
				"SaveSelected")
		})
		group.POST("/selected_all", func(r *ghttp.Request) {
			BindHandlerVersion(r, versionName, controllerMaps, defaultVersionName,
				"SelectedAll")
		})

		group.POST("/change_promotion", func(r *ghttp.Request) {
			BindHandlerVersion(r, versionName, controllerMaps, defaultVersionName, "ChangePromotion")
		})

		group.POST("/change", func(r *ghttp.Request) {
			BindHandlerVersion(r, versionName, controllerMaps, defaultVersionName, "Change")
		})
	})
	s.Group("/cart", func(group *ghttp.RouterGroup) {
		controllerMaps := map[string]interface{}{
			"1.0.0": new(controllers.CartController),
		}
		group.Middleware(func(r *ghttp.Request) {
			token.CheckToken(r)
			r.Middleware.Next()
		})
		group.GET("/my", func(r *ghttp.Request) {
			BindHandlerVersion(r, versionName, controllerMaps, defaultVersionName, "My")
		})
		// 获取购物车数量
		group.GET("/count", func(r *ghttp.Request) {
			BindHandlerVersion(r, versionName, controllerMaps, defaultVersionName,
				"Count")
		})
	})
	// 商品
	s.Group("/goods", func(group *ghttp.RouterGroup) {
		group.Middleware(MiddlewareIsMemberLoginNoCheck)
		controllerMaps := map[string]interface{}{
			"1.0.0": new(controllers.GoodsController),
		}
		group.GET("/list", func(r *ghttp.Request) {
			BindHandlerVersion(r, versionName, controllerMaps, defaultVersionName, "List")
		})
		group.GET("/details", func(r *ghttp.Request) {
			BindHandlerVersion(r, versionName, controllerMaps, defaultVersionName, "Details")
		})
		group.GET("/content", func(r *ghttp.Request) {
			BindHandlerVersion(r, versionName, controllerMaps, defaultVersionName, "Content")
		})
		// 热门搜索
		group.GET("/hot_search", func(r *ghttp.Request) {
			BindHandlerVersion(r, versionName, controllerMaps,
				defaultVersionName, "HotSearch")
		})
		// 列表时，筛选数据
		group.GET("/filter_data", func(r *ghttp.Request) {
			BindHandlerVersion(r, versionName, controllerMaps,
				defaultVersionName, "FilterData")
		})

		group.GET("/tuan_details", func(r *ghttp.Request) {
			BindHandlerVersion(r, versionName, controllerMaps, defaultVersionName, "TuanDetails")
		})
	})
	s.Group("/goods", func(group *ghttp.RouterGroup) {
		controllerMaps := map[string]interface{}{
			"1.0.0": new(controllers.GoodsController),
		}
		group.Middleware(MiddlewareIsMemberLogin)
		group.GET("/collection", func(r *ghttp.Request) {
			BindHandlerVersion(r, versionName, controllerMaps,
				defaultVersionName, "Collection")
		})
	})
	// 地址
	s.Group("/address", func(group *ghttp.RouterGroup) {
		controllerMaps := map[string]interface{}{
			"1.0.0": new(controllers.AddressController),
		}
		group.Middleware(MiddlewareIsMemberLogin)
		group.GET("/list", func(r *ghttp.Request) {
			BindHandlerVersion(r, versionName, controllerMaps, defaultVersionName, "List")
		})
		group.GET("/details", func(r *ghttp.Request) {
			BindHandlerVersion(r, versionName, controllerMaps, defaultVersionName, "Details")
		})
		group.POST("/add", func(r *ghttp.Request) {
			BindHandlerVersion(r, versionName, controllerMaps,
				defaultVersionName, "Add")
		})
		group.POST("/save", func(r *ghttp.Request) {
			BindHandlerVersion(r, versionName, controllerMaps,
				defaultVersionName, "Save")
		})

		group.GET("/del", func(r *ghttp.Request) {
			BindHandlerVersion(r, versionName, controllerMaps,
				defaultVersionName, "Del")
		})
	})
	// 订单
	s.Group("/order", func(group *ghttp.RouterGroup) {
		controllerMaps := map[string]interface{}{
			"1.0.0": new(controllers.OrderController),
		}
		group.Middleware(MiddlewareIsMemberLogin)
		group.GET("/list", func(r *ghttp.Request) {
			BindHandlerVersion(r, versionName, controllerMaps, defaultVersionName, "List")
		})
		group.GET("/details", func(r *ghttp.Request) {
			BindHandlerVersion(r, versionName, controllerMaps, defaultVersionName, "Details")
		})
		// 预下单数据获取
		group.POST("/create", func(r *ghttp.Request) {
			BindHandlerVersion(r, versionName, controllerMaps,
				defaultVersionName, "Create")
		})
		group.POST("/submit", func(r *ghttp.Request) {
			BindHandlerVersion(r, versionName, controllerMaps,
				defaultVersionName, "Submit")
		})
		group.POST("/check_submit_ok", func(r *ghttp.Request) {
			BindHandlerVersion(r, versionName, controllerMaps,
				defaultVersionName, "CheckSubmitOk")
		})
		group.GET("/del", func(r *ghttp.Request) {
			BindHandlerVersion(r, versionName, controllerMaps,
				defaultVersionName, "Del")
		})

		group.GET("/pay", func(r *ghttp.Request) {
			BindHandlerVersion(r, versionName, controllerMaps, defaultVersionName,
				"Pay")
		})

		group.POST("/submit_pay", func(r *ghttp.Request) {
			BindHandlerVersion(r, versionName, controllerMaps, defaultVersionName,
				"SubmitPay")
		})

		group.POST("/use_coupon", func(r *ghttp.Request) {
			BindHandlerVersion(r, versionName, controllerMaps, defaultVersionName,
				"UseCoupon")
		})

		group.GET("/order_sub_aftersale", func(r *ghttp.Request) {
			BindHandlerVersion(r, versionName, controllerMaps, defaultVersionName,
				"OrderSubAftersale")
		})

		group.POST("/order_sub_aftersale", func(r *ghttp.Request) {
			BindHandlerVersion(r, versionName, controllerMaps, defaultVersionName,
				"SaveOrderSubAftersale")
		})

		group.POST("/close", func(r *ghttp.Request) {
			BindHandlerVersion(r, versionName, controllerMaps, defaultVersionName,
				"Close")
		})

		group.POST("/confirm", func(r *ghttp.Request) {
			BindHandlerVersion(r, versionName, controllerMaps, defaultVersionName,
				"Confirm")
		})

		group.GET("/logistics", func(r *ghttp.Request) {
			BindHandlerVersion(r, versionName, controllerMaps, defaultVersionName,
				"Logistics")
		})
	})
	// 分类
	s.Group("/classify", func(group *ghttp.RouterGroup) {
		controllerMaps := map[string]interface{}{
			"1.0.0": new(controllers.ClassifyController),
		}
		group.GET("/list", func(r *ghttp.Request) {
			BindHandlerVersion(r, versionName, controllerMaps, defaultVersionName, "List")
		})
	})

	s.Group("/member", func(group *ghttp.RouterGroup) {
		controllerMaps := map[string]interface{}{
			"1.0.0": new(controllers.MemberController),
		}
		group.Middleware(MiddlewareIsMemberLogin)

		group.POST("/wechat_bind", func(r *ghttp.Request) {
			BindHandlerVersion(r, versionName, controllerMaps, defaultVersionName,
				"WechatBind")
		})

		group.GET("/my_favorite", func(r *ghttp.Request) {
			BindHandlerVersion(r, versionName, controllerMaps, defaultVersionName,
				"MyFavorite")
		})

		group.POST("/del_favorite", func(r *ghttp.Request) {
			BindHandlerVersion(r, versionName, controllerMaps, defaultVersionName,
				"DelFavorite")
		})

		group.GET("/my_balance", func(r *ghttp.Request) {
			BindHandlerVersion(r, versionName, controllerMaps, defaultVersionName,
				"MyBalance")
		})

		group.GET("/my_searchs", func(r *ghttp.Request) {
			BindHandlerVersion(r, versionName, controllerMaps, defaultVersionName,
				"MySearchs")
		})

		group.GET("/claer_my_searchs", func(r *ghttp.Request) {
			BindHandlerVersion(r, versionName, controllerMaps, defaultVersionName,
				"ClaerMySearchs")
		})

		group.GET("/my_refund_list", func(r *ghttp.Request) {
			BindHandlerVersion(r, versionName, controllerMaps, defaultVersionName,
				"MyRefundList")
		})
	})

	s.Group("/member", func(group *ghttp.RouterGroup) {
		controllerMaps := map[string]interface{}{
			"1.0.0": new(controllers.MemberController),
		}
		group.Middleware(func(r *ghttp.Request) {
			token.CheckToken(r)
			r.Middleware.Next()
		})
		group.GET("/index", func(r *ghttp.Request) {
			BindHandlerVersion(r, versionName, controllerMaps, defaultVersionName,
				"Index")
		})
	})

	s.Group("/coupon", func(group *ghttp.RouterGroup) {
		controllerMaps := map[string]interface{}{
			"1.0.0": new(controllers.CouponController),
		}
		group.Middleware(MiddlewareIsMemberLogin)

		group.GET("/index", func(r *ghttp.Request) {
			BindHandlerVersion(r, versionName, controllerMaps, defaultVersionName,
				"Index")
		})

		group.GET("/receive", func(r *ghttp.Request) {
			BindHandlerVersion(r, versionName, controllerMaps, defaultVersionName,
				"Receive")
		})

		group.GET("/my", func(r *ghttp.Request) {
			BindHandlerVersion(r, versionName, controllerMaps, defaultVersionName,
				"My")
		})

		group.GET("/details", func(r *ghttp.Request) {
			BindHandlerVersion(r, versionName, controllerMaps, defaultVersionName,
				"Details")
		})
	})

	s.Group("/fenxiao", func(group *ghttp.RouterGroup) {
		controllerMaps := map[string]interface{}{
			"1.0.0": new(controllers.FenxiaoController),
		}
		group.Middleware(MiddlewareIsMemberLogin)

		group.GET("/index", func(r *ghttp.Request) {
			BindHandlerVersion(r, versionName, controllerMaps, defaultVersionName,
				"Index")
		})

		group.GET("/get_member_withdraw", func(r *ghttp.Request) {
			BindHandlerVersion(r, versionName, controllerMaps, defaultVersionName,
				"GetMemberWithdraw")
		})

		group.POST("/withdraw_apply", func(r *ghttp.Request) {
			BindHandlerVersion(r, versionName, controllerMaps, defaultVersionName,
				"SubmitWithdrawApply")
		})

		group.GET("/bill_list", func(r *ghttp.Request) {
			BindHandlerVersion(r, versionName, controllerMaps, defaultVersionName,
				"BillList")
		})

		group.GET("/withdraw_list", func(r *ghttp.Request) {
			BindHandlerVersion(r, versionName, controllerMaps, defaultVersionName,
				"WithdrawList")
		})

		group.GET("/goods_list", func(r *ghttp.Request) {
			BindHandlerVersion(r, versionName, controllerMaps, defaultVersionName,
				"GoodsList")
		})

		group.GET("/goods_follow_change", func(r *ghttp.Request) {
			BindHandlerVersion(r, versionName, controllerMaps, defaultVersionName,
				"GoodsFollowChange")
		})

		group.GET("/goods_follow_list", func(r *ghttp.Request) {
			BindHandlerVersion(r, versionName, controllerMaps, defaultVersionName,
				"GoodsFollowList")
		})

		group.POST("/goods_follow_del", func(r *ghttp.Request) {
			BindHandlerVersion(r, versionName, controllerMaps, defaultVersionName,
				"GoodsFollowDel")
		})

		group.GET("/team_list", func(r *ghttp.Request) {
			BindHandlerVersion(r, versionName, controllerMaps, defaultVersionName,
				"TeamList")
		})

		group.GET("/order_list", func(r *ghttp.Request) {
			BindHandlerVersion(r, versionName, controllerMaps, defaultVersionName,
				"OrderList")
		})

		group.GET("/my_promote_code_img", func(r *ghttp.Request) {
			BindHandlerVersion(r, versionName, controllerMaps, defaultVersionName,
				"MyPromoteCodeImg")
		})
	})

	s.Group("/article", func(group *ghttp.RouterGroup) {
		controllerMaps := map[string]interface{}{
			"1.0.0": new(controllers.ArticleController),
		}
		group.Middleware(MiddlewareIsMemberLogin)
		group.GET("/content", func(r *ghttp.Request) {
			BindHandlerVersion(r, versionName, controllerMaps, defaultVersionName, "Content")
		})
	})

	// 注册 eventBus ，解耦用
	regEventBus()
}
